package de.ubschmidt.capi;

import java.nio.ByteBuffer;

/**
 * List of predefined Compatibility Information Profiles (CIP).
 *
 * <p>The purpose of parameter CIP Value is to identify a complete profile of
 * the compatibility information Bearer Capability, Low Layer Compatibility
 * and High Layer Compatibility. With this parameter, standard applications
 * are not required to do complex coding and decoding of these individual
 * information elements.
 *
 * <p>Some of the CIP values only define a Bearer Capability (CIP 1 to 9),
 * and some define a combination of Bearer Capability and High Layer
 * Compatibility (CIP 16 to 28). A Low Layer Compatibility information
 * element is not defined by the CIP, but must be provided by the
 * application if necessary.
 */
public enum Cip
        implements ByteBufferWritable {

    /**
     * Unspecified / Any.
     *
     * <p>Only applicable in a LISTEN_REQ.
     */
    ANY(0),

    /**
     * Speech
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: speech
     * <li>Transfer mode: circuit mode
     * <li>Information transfer rate: 64 kbit/s
     * <li>User information Layer 1 protocol: G.711
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x03, 0x80, 0x90, 0xA3&gt; or<br>
     * &lt;0x04, 0x03, 0x80, 0x90, 0xA2&gt; (see note)
     * </blockquote>
     *
     * Note: This coding applies to ISDN with A-Law default coding for
     * speech/audio. For ISDN with \u03BC-Law default coding, the corresponding
     * values are used.
     */
    SPEECH(1),

    /**
     * Unrestricted digital information.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: unrestricted digital information
     * <li>Transfer mode: circuit mode
     * <li>Information transfer rate: 64 kbit/s
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x02, 0x88, 0x90&gt;
     * </blockquote>
     */
    UNRESTRICTED_DIGITAL_INFORMATION(2),

    /**
     * Restricted digital information.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: restricted digital information
     * <li>Transfer mode: circuit mode
     * <li>Information transfer rate: 64 kbit/s
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x02, 0x89, 0x90&gt;
     * </blockquote>
     */
    RESTRICTED_DIGITAL_INFORMATION(3),

    /**
     * 3.1 kHz audio.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: 3.1 kHz audio
     * <li>Transfer mode: circuit mode
     * <li>Information transfer rate: 64 kbit/s
     * <li>User information Layer 1 protocol: G.711
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x03, 0x90, 0x90, 0xA3&gt; or<br>
     * &lt;0x04, 0x03, 0x80, 0x90, 0xA2&gt; (see note)
     * </blockquote>
     *
     * Note: This coding applies to ISDN with A-Law default coding for
     * speech/audio. For ISDN with \u03BC-Law default coding, the corresponding
     * values are used.
     */
    AUDIO_3100HZ(4),

    /**
     * 7 kHz audio.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: unrestricted digital information
     *     with tones/announcements
     * <li>Transfer mode: circuit mode
     * <li>Information transfer rate: 64 kbit/s
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x02, 0x91, 0x90&gt;
     * </blockquote>
     */
    AUDIO_7KHZ(5),

    /**
     * Video.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: video
     * <li>Transfer mode: circuit mode
     * <li>Information transfer rate: 64 kbit/s
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x02, 0x98, 0x90&gt;
     * </blockquote>
     */
    VIDEO(6),

    /**
     * Packet mode.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: unrestricted digital information
     * <li>Transfer mode: packet mode
     * <li>Information transfer rate: packet mode
     * <li>Layer 2 protocol: X.25 Layer 2
     * <li>Layer 3 protocol: X.25 Layer 3
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x04, 0x88, 0xC0, 0xC6, 0xE6&gt;
     * </blockquote>
     */
    PACKET_MODE(7),

    /**
     * 56 kbit/s rate adaptation.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: unrestricted digital information
     * <li>Transfer mode: circuit mode
     * <li>Layer 1 protocol: CCITT standardized rate adaptation V.110/X.30
     * <li>Information transfer rate: packet mode
     * <li>Rate: 56 kbit/s
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * <&lt;0x04, 0x04, 0x88, 0x90, 0x21, 0x8F&gt;
     * </blockquote>
     */
    RATE_ADAPTION_56KBITPS(8),

    /**
     * Unrestricted digital information with tones/announcements.
     *
     * <p>
     * Bearer capability:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Information transfer capability: unrestricted digital information
     *     with tones/announcements
     * <li>Transfer mode: circuit mode
     * <li>Information transfer rate: 64 kbit/s
     * <li>Layer 1 protocol: H.221, H.242
     * </ul>
     *
     * Coding of BC:
     * <blockquote>
     * &lt;0x04, 0x03, 0x91, 0x90, 0xA5&gt;
     * </blockquote>
     */
    UNRESTRICTED_DIGITAL_INFORMATION_WITH_TONES_AND_ANNOUNCEMENTS(9),

    /**
     * Telephony.
     *
     * <p>
     * Bearer Capability as for CIP 1.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Telephony
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0x81&gt;
     * </blockquote>
     */
    TELEPHONY(16),

    /**
     * Group 2/3 facsimile.
     *
     * <p>
     * Bearer Capability as for CIP 4.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Group 2/3 facsimile
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0x84&gt;
     * </blockquote>
     */
    GROUP_2_3_FACSIMILE(17),

    /**
     * Group 4 facsimile Class 1.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Group 4 facsimile Class 1
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0xA1&gt;
     * </blockquote>
     */
    GROUP_4_FACSIMILE_CLASS_1(18),

    /**
     * Teletex service basic and mixed mode and Group 4 facsimile
     * service Classes II and III.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification. Teletex service
     *     and Group 4 facsimile service
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0xA4&gt;
     * </blockquote>
     */
    TELETEX_SERVICE_BASIC_AND_MIXED_MODE(19),

    /**
     * Teletex service basic and processable mode.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification. Teletex service
     *     basic and processable mode
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0xA8&gt;
     * </blockquote>
     */
    TELETEX_SERVICE_BASIC_AND_PROCESSABLE_MODE(20),

    /**
     * Teletex service basic mode.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification. Teletex service
     *     basic mode
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0xB1<&gt;
     * </blockquote>
     */
    TELETEX_SERVICE_BASIC_MODE(21),

    /**
     * International interworking for Videotex.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification. International interworking
     *     for Videotex
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0xB2&gt;
     * </blockquote>
     */
    INTERNATIONAL_INTERWORKING_FOR_VIDEOTEX(22),

    /**
     * Telex.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Telex
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * <0x7D, 0x02, 0x91, 0xB5>
     * </blockquote>
     */
    TELETEX(23),

    /**
     * Message Handling Systems in accordance with X.400.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Message Handling
     *     Systems (in accordance with X.400)
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0xB8&gt;
     * </blockquote>
     */
    X400(24),

    /**
     * OSI application in accordance with X.200.
     *
     * <p>
     * Bearer Capability as for CIP 2.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: OSI application in
     *     accordance with X.200
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0xC1&gt;
     * </blockquote>
     */
    X200(25),

    /**
     * 7 kHz telephony.
     *
     * <p>
     * Bearer Capability as for CIP 9.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Telephony
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x02, 0x91, 0x81&gt;
     * </blockquote>
     */
    TELEPHONY_7KHZ(26),

    /**
     * Video telephony, first connection.
     *
     * <p>
     * Bearer Capability as for CIP 9.
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Video telephony (F.721)
     * <li>Extended high layer characteristics identification: Capability
     *     set of initial channel per H.221
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x03, 0x91, 0x60, 0x01&gt;
     * </blockquote>
     */
    VIDEO_TELEPHONY_FIRST_CONNECTION(27),

    /**
     * Video telephony, second connection.
     *
     * <p>
     * Bearer Capability as for CIP 2
     *
     * <p>
     * High Layer Compatibility:
     * <ul>
     * <li>Coding standard: CCITT
     * <li>Interpretation: First characteristics identification is to be used
     * <li>Presentation: High layer protocol profile
     * <li>High layer characteristics identification: Video telephony
     *     (Rec. F.721)
     * <li>Extended high layer characteristics identification: Capability
     *     set of subsequent channel per H.221
     * </ul>
     *
     * Coding of HLC:
     * <blockquote>
     * &lt;0x7D, 0x03, 0x91, 0x60, 0x02&gt;
     * </blockquote>
     */
    VIDEO_TELEPHONY_SECOND_CONNECTION(28);

    /**
     * Create a enum constant holding the binary representation of the enum.
     *
     * @param value The binary representation of the enum.
     */
    Cip(final int value) {
        _value = (short) value;
    }

    /** The binary representation of this enum. */
    private short _value;

    /**
     * Retrieve the binary representation of this CIP value.
     *
     * @return The binary representation of this CIP value.
     */
    public short getValue() {
        return _value;
    }

    /**
     * Retrieve the enum representing the specified value.
     *
     * @param value The value of the requested enum.
     * @return The Cip matching the specified value.
     * @throws IllegalArgumentException An {@code IllegalArgumentException}
     *                                  is thrown if the value is not known.
     */
    public static Cip valueOf(final short value)
            throws IllegalArgumentException {
        Cip [] enums = values();

        for (int i = 0; i < enums.length; ++i) {
            if (enums[i].getValue() == value) {
                return enums[i];
            }
        }

        throw new IllegalArgumentException(
                String.format("unknown value 0x%04x for CIP", value));
    }

    /** {@inheritDoc} */
    public void writeTo(final ByteBuffer byteBuffer) {
        byteBuffer.putShort(_value);
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        String s = String.format("%s(%s)",
                name(),
                getValue());

        return s;
    }
}
